package dev.pda.display.presentation.page.util

import dev.pda.display.data.message.entity.Control
import dev.pda.display.data.message.entity.ResourceMessage
import dev.pda.display.presentation.page.model.*
import dev.pda.display.presentation.page.model.widget.ImageWidget
import dev.pda.display.presentation.page.model.widget.TextWidget
import dev.pda.display.presentation.page.model.widget.UrlWidget
import dev.pda.display.presentation.page.model.widget.VideoWidget

object DataMapper {

    fun convertToTextWidget(control: Control): TextWidget {
        return TextWidget().apply {
            item = textBeanConvertToTextVM(control)
            type = control.type
            name = control.name
            width = control.width
            height = control.height
            x = control.x
            y = control.y
            zIndex = control.zindex
        }
    }

    fun convertToImageWidget(control: Control): ImageWidget {
        return ImageWidget().apply {
            item = imageBeanConvertToImageVM(control)
            type = control.type
            name = control.name
            width = control.width
            height = control.height
            x = control.x
            y = control.y
            zIndex = control.zindex
        }
    }

    fun convertToVideoWidget(control: Control): VideoWidget {
        return VideoWidget().apply {
            item = videoBeanConvertToVideoVM(control)
            type = control.type
            name = control.name
            width = control.width
            height = control.height
            x = control.x
            y = control.y
            zIndex = control.zindex
        }
    }

    fun convertToUrlWidget(control: Control): UrlWidget {
        return UrlWidget().apply {
            item = urlBeanConvertToUrlVM(control)
            type = control.type
            name = control.name
            width = control.width
            height = control.height
            x = control.x
            y = control.y
            zIndex = control.zindex
        }
    }

    private fun textBeanConvertToTextVM(control: Control): TextVM? {
        return TextVM(
            control.textBean?.content ?: "",
            control.textBean?.fontSize ?: 16.0,
            control.textBean?.fontColor ?: "#FFFFFFFF",
            control.type,
            control.name,
            control.width,
            control.height,
            control.x,
            control.y,
            control.zindex

        )
    }

    private fun videoBeanConvertToVideoVM(control: Control): VideoVM? {
        return VideoVM(
            control.videoBean?.filePath ?: "",
            getFileSuffixName(control.videoBean?.filePath ?: ""),
            control.type,
            control.name,
            control.width,
            control.height,
            control.x,
            control.y,
            control.zindex
        )
    }

    private fun imageBeanConvertToImageVM(control: Control): ImageVM? {
        return ImageVM(
            control.imageBean?.imgUrl ?: "",
            control.type,
            control.name,
            control.width,
            control.height,
            control.x,
            control.y,
            control.zindex
        )
    }

    private fun urlBeanConvertToUrlVM(control: Control): UrlVM? {
        return UrlVM(
            control.urlBean?.content ?: "",
            control.urlBean?.fontSize ?: 16.0,
            control.urlBean?.fontColor ?: "#FFFFFFFF",
            control.type,
            control.name,
            control.width,
            control.height,
            control.x,
            control.y,
            control.zindex
        )
    }

    fun resourceMessageConvertToProgramVM(resourceMessage: ResourceMessage): ProgramVM? {
        val widgetModels = WidgetParser().parseWidget(resourceMessage)
        return ProgramVM(
            widgetModels = widgetModels,
            resources = resourceListConvertToResourceVM(
                resourceMessage.localServerIp,
                resourceMessage.resourcesList
            ),
            projectName = resourceMessage.projectName,
            screenWidth = resourceMessage.disWidth,
            screenHeight = resourceMessage.disHeight,
            projectType = resourceMessage.projectType,
            centerControlIp = resourceMessage.centerControlIp,
            localServerIp = resourceMessage.localServerIp
        )
    }

    private fun resourceListConvertToResourceVM(
        localServerIp: String,
        resourceList: List<List<String>>
    ): List<ResourceVM> {
        val resourceVMList = ArrayList<ResourceVM>()
        resourceList.forEach {
            val url = localServerIp + it[0]
            resourceVMList.add(ResourceVM(url, it[1], getFileSuffixName(url)))
        }
        return resourceVMList
    }

    private fun getFileSuffixName(url: String): String {
        return url.substring(url.lastIndexOf("/") + 1)
    }
}